package com.beermanager.facebook;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.beermanager.activities.MainActivity;
import com.beermanager.domain.Bar;
import com.beermanager.domain.Beer;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.ServiceListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Facebook.DialogListener;

public class FacebookConnection {
	static Facebook facebook = new Facebook("290266334405284");
	private SharedPreferences mPrefs;
	private static Activity mContext;
	private static Bar currentBar;
	private static Beer currentBeer;

    public FacebookConnection(MainActivity mainActivity) {
    	mContext = mainActivity;
        /*
           * Get existing access_token if any
           */
        mPrefs = mainActivity.getPreferences(Context.MODE_PRIVATE);
        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);
        if (access_token != null) {
            facebook.setAccessToken(access_token);
        }
        if (expires != 0) {
            facebook.setAccessExpires(expires);
        }

        /*
           * Only call authorize if the access_token has expired.
           */
        if (!facebook.isSessionValid()) {

            facebook.authorize(mContext, new String[] {"publish_checkins", "publish_stream"}, new DialogListener() {
                @Override
                public void onComplete(Bundle values) {
                    SharedPreferences.Editor editor = mPrefs.edit();
                    editor.putString("access_token", facebook.getAccessToken());
                    editor.putLong("access_expires",
                            facebook.getAccessExpires());
                    editor.commit();
                }

                @Override
                public void onFacebookError(FacebookError error) {
                }

                @Override
                public void onError(DialogError e) {
                }

                @Override
                public void onCancel() {
                }
            });
        }
    }

	public void authorizeCallback(int requestCode, int resultCode, Intent data) {
		facebook.authorizeCallback(requestCode, resultCode, data);
	}

	public boolean extendAccessTokenIfNeeded(Context context,
			ServiceListener serviceListener) {
		return facebook.extendAccessTokenIfNeeded(context, serviceListener);
	}
	
	public static void setBar(Bar bar) {
		currentBar = bar;
	}

	public static void setBeer(Beer beer) {
		currentBeer = beer;
	}
	
	public static void setContext(Activity activity) {
		mContext = activity;
	}
	
	public static boolean readyToPost() {
		return currentBar != null && currentBeer != null;
	}
	
	public static void postFacebookMessage(String rating) {
		Bundle params = new Bundle();
		
		//params.putString("access_token", facebook.getAccessToken());
		//params.putString("place", "203682879660695"); //128709870533684
		//params.putString("coordinates", "{\"longitude\":\"123.123\",\"latitude\":\"123.123\"}");
		//params.putString("Message","Acabei de tomar uma Cerveja no " + currentBar.getName());
		params.putString("picture", "http://icons.iconarchive.com/icons/iconshock/real-vista-food/256/beer-icon.png");
		params.putString("name", "Cerveja!");
		params.putString("caption", rating);
		
		String description = "Acabei de tomar uma ";
		description += currentBeer == null? "cerveja" : currentBeer.getName();
		description += " no ";
		description += currentBar == null? "bar" : currentBar.getName();
		params.putString("description", description);
		
		facebook.dialog(mContext, "feed", params, new DialogListener() {
			
			@Override
			public void onFacebookError(FacebookError e) {
				// TODO Auto-generated method stub
				System.out.println("onFacebookError");
				
			}
			
			@Override
			public void onError(DialogError e) {
				// TODO Auto-generated method stub
				System.out.println("onError");
				
			}
			
			@Override
			public void onComplete(Bundle values) {
				// TODO Auto-generated method stub
				System.out.println("onComplete");
				
			}
			
			@Override
			public void onCancel() {
				// TODO Auto-generated method stub
				System.out.println("onCancel");
				
			}
		});
	}
}
